package com.xm.xmapp.tools;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

import com.xm.xmapp.app.Constants;

public class Des4 {

	// 密钥

	private static byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0 };

	public static String encode(String plainText) {
		try {

			IvParameterSpec zeroIv = new IvParameterSpec(iv);
			SecretKeySpec key = new SecretKeySpec(
					Constants.secretKey.getBytes(), "DES");
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
			byte[] encryptedData = cipher.doFinal(plainText.getBytes());
			return Base64.encodeToString(encryptedData, 1);
		} catch (Exception e) {
			return null;
		}
	}

	public static String decode(String encryptText) {
		try {
			IvParameterSpec zeroIv = new IvParameterSpec(iv);
			SecretKeySpec key = new SecretKeySpec(
					Constants.secretKey.getBytes(), "DES");
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
			byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, 1));
			return new String(decryptData);
		} catch (Exception e) {
			return null;
		}
	}
}
